package game.gui;

/**
 *
 * @author Gab'z
 */
import game.gui.panelconversation.PanelConversation;
import game.gui.panelinventaire.PanelInventaire;
import game.gui.panelvue.PanelVue;
import javax.swing.JLayeredPane;

public class PanelJeu extends JLayeredPane {

    private static PanelJeu panelJeu = null;
    private static PanelVue panelVue = null;
    private static PanelInventaire panelInventaire = null;
    private static PanelConversation panelConversation = null;
    private static PanelBlack blackPanel = null;
    private static PanelTitlePanelBlack panelTitleBlackPanel = null;

    private PanelJeu() {
        this.setLayout(null);
        this.setSize(980, 810);

        // Panel Vue
        panelVue = PanelVue.getPanelVue();
        panelVue.setLocation(0, 0);
        this.add(panelVue, new Integer(0));

        // Panel Inventaire
        panelInventaire = PanelInventaire.getPanelInventaire();
        panelInventaire.setLocation(690, 0);
        this.add(panelInventaire, new Integer(0));

        // Panel Conversation
        panelConversation = PanelConversation.getPanelConversation();
        panelConversation.setLocation(0, 565);
        this.add(panelConversation, new Integer(0));

        // Panel Black
        blackPanel = PanelBlack.getPanelBlack();
        blackPanel.setLocation(0, 0);
        blackPanel.setVisible(false);
        this.add(blackPanel, new Integer(1));

        // Panel title Black
        panelTitleBlackPanel = panelTitleBlackPanel.getPanelTitlePanelBlack("");
        panelTitleBlackPanel.setLocation(0, 0);
        panelTitleBlackPanel.setVisible(false);
        this.add(panelTitleBlackPanel, new Integer(3));
    }

    public static PanelJeu getPanelJeu() {
        if (panelJeu == null) {
            panelJeu = new PanelJeu();
        }
        return panelJeu;
    }

    public static void setPanelVue(String idPlan) {
        panelVue.setVue(idPlan);
    }

    public void chargerPanelInventaire() {
        panelInventaire = PanelInventaire.getPanelInventaire();
    }

    public static void fadeToBlack(String string) throws InterruptedException {
        // on en prend un nouveau
        blackPanel = PanelBlack.getPanelBlack();
        blackPanel.setVisible(true);
        panelTitleBlackPanel = PanelTitlePanelBlack.getPanelTitlePanelBlack(string);
        panelTitleBlackPanel.setVisible(true);

        //  on affiche




        panelJeu.add(blackPanel, new Integer(1));
        // on set de transparent vers plein
        while (blackPanel.getAlpha() < 255) {
            Thread.sleep(10);
            blackPanel.increaseAlpha();
            //on actualise
            panelJeu.refresh();
        }
    }

    public static void fadeIn() throws InterruptedException {
        if (blackPanel != null) {

            // on set de plein vers transparent
            while (blackPanel.getAlpha() > 0) {
                Thread.sleep(1);
                blackPanel.decreaseAlpha();
                //on actualise
                panelJeu.refresh();
            }

            //on retire de la vue
            panelJeu.remove(panelTitleBlackPanel);
            panelJeu.remove(blackPanel);
            // on set = null celui de PanelBlack (pas directement ici)
            PanelBlack.resetPanelBlack();
            PanelTitlePanelBlack.resetPanelTitlePanelBlack();
        } else {
            System.err.println("ERREUR LORS DU FADE IN : blackPanel est null\n Utilisation trop tot de cette fonction");
        }

    }

    private void refresh() {
        blackPanel.repaint();
        blackPanel.validate();
        this.repaint();
        this.validate();
    }
}
